package com.xiaotu.impRunnable;

import com.xiaotu.exThread.testThread02;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @Author xiaotu
 * @Description //TODO
 * @Date 2022/3/24 16:29
 * @ClassName testThread02
 * @Version 1.0
 **/
//练习Thread,实现多线程同步下载图片
public class testThread03 implements Runnable{
    private  String url;
    private String  name;
    public testThread03(String url,String name){
        this.name = name;
        this.url = url;
    }
    @Override
    public void run() {
        DownLoader downLoader = new DownLoader();
        downLoader.downloads(url, name);
        System.out.println("下载了文件名为"+name);
    }

    public static void main(String[] args) {
        testThread03 t1 = new testThread03("https://img2.baidu.com/it/u=2090606195,1473750087&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500","美女.jpg");
        testThread03 t2 = new testThread03("https://img2.baidu.com/it/u=1790834130,1952230725&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500","叮当猫.jpg");
        testThread03 t3 = new testThread03("https://img2.baidu.com/it/u=1777042033,1463932871&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400","邮政.jpg");
        Thread thread01 = new Thread(t1);
        Thread thread02 = new Thread(t2);
        Thread thread03 = new Thread(t3);
        thread01.start();
        thread02.start();
        thread03.start();
        /**
         * 下载了文件名为邮政.jpg
         * 下载了文件名为美女.jpg
         * 下载了文件名为叮当猫.jpg
         */
    }
}

class DownLoader{
    //下载方法
    public void downloads(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url), new File(name));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
